﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RenChangYou.Common;

namespace DaoYou.Web.Account
{
    public partial class UserCenter : DaoYou.Web.Guider.Account.PageBase
    {
        protected string Province { get; set; }
        protected string City { get; set; }
        protected string Area { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RenChangYou.BLL.DY_User bllUser = new RenChangYou.BLL.DY_User();
                RenChangYou.Model.DY_User model = bllUser.GetModelByCache(base.Account);

                if (model != null)
                {
                    this.txtAddress.Value = model.Address;
                    this.txtBirthday.Value = model.Birthday.HasValue ? model.Birthday.Value.ToShortDateString() : string.Empty;
                    this.City = model.City;
                    this.Area = model.District;
                    this.Province = model.Province;
                    this.ddlSex.SelectedValue = model.Sex;
                    this.txtEmail.Value = model.Email;
                    this.txtFullName.Value = model.FullName;
                    this.txtHometown.Value = model.HomeTown;
                    this.txtMobile.Value = model.Mobile;
                }
            }

            
            

            RenChangYou.BLL.DY_GuideIntroduction bllIntro = new RenChangYou.BLL.DY_GuideIntroduction();
            RenChangYou.Model.DY_GuideIntroduction intro = bllIntro.GetModelByCache(base.Account);
            if (intro != null)
            {
                this.txtIntroduction.Text = intro.Introduction;
            }
        }

        protected void lbtnApplyDaoYou_Click(object sender, EventArgs e)
        {
            RenChangYou.BLL.DY_User bll = new RenChangYou.BLL.DY_User();
            RenChangYou.Model.DY_User model = bll.GetModelByCache(base.Account);
            model.UserType = 1;
            bll.Update(model);

            base.CheckIsLogin();

            HttpContext.Current.Response.Redirect("UserInfoEdit.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                #region 保存 DY_User
                RenChangYou.BLL.DY_User bll = new RenChangYou.BLL.DY_User();
                RenChangYou.Model.DY_User model = bll.GetModelByCache(base.Account);
                model.Address = this.txtAddress.Value;
                if (PageValidate.IsDateTime(this.txtBirthday.Value))
                    model.Birthday = Convert.ToDateTime(this.txtBirthday.Value);
                model.City = Request["ctl00$MainContent$ddlCity"];
                model.Country = "中国";
                model.District = Request["ctl00$MainContent$ddlArea"];
                model.Email = this.txtEmail.Value;
                model.FullName = this.txtFullName.Value;
                model.HomeTown = this.txtHometown.Value;
                model.Mobile = this.txtMobile.Value;
                model.Province = Request["ctl00$MainContent$ddlProvince"];
                model.Sex = this.ddlSex.SelectedValue;
                bll.Update(model);
                #endregion

                #region 保存 DY_GuideIntroduction

                if (!string.IsNullOrWhiteSpace(this.txtIntroduction.Text))
                {
                    RenChangYou.BLL.DY_GuideIntroduction bllIntro = new RenChangYou.BLL.DY_GuideIntroduction();
                    RenChangYou.Model.DY_GuideIntroduction modelIntro = bllIntro.GetModelByCache(base.Account);

                    if (modelIntro == null)
                    {
                        bllIntro.Add(new RenChangYou.Model.DY_GuideIntroduction()
                        {
                            CreatedTime = DateTime.Now,
                            Introduction = this.txtIntroduction.Text,
                            IsInUse = true,
                            UserName = base.Account,
                            ViewCount = 1
                        });
                    }
                    else
                    {
                        modelIntro.IsInUse = false;
                        bllIntro.Update(modelIntro);

                        bllIntro.Add(new RenChangYou.Model.DY_GuideIntroduction()
                        {
                            CreatedTime = DateTime.Now,
                            Introduction = this.txtIntroduction.Text,
                            IsInUse = true,
                            UserName = base.Account,
                            ViewCount = 1
                        });
                    }
                }

                #endregion

                Common.AlertMessageAndRedirect("提交成功", Page.ResolveUrl("~/Account/UserCenter.aspx"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}